package com.controller.Front;


import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.common.utils.DateUtils;
import com.common.utils.base.UuidUtil;
import com.controller.base.BaseController;
import com.entity.Product;
import com.entity.ProductDocking;
import com.entity.Users;
import com.service.DockingService;
import com.service.ProductService;
//产品上传
@Controller
@RequestMapping("/FrontProduct/")
public class FrontProductController  extends BaseController<Object>{
	
	//产品
	@Autowired
	private ProductService ProductSer;
	
	
	//对接
	@Autowired
	private  DockingService DockingServ;
	/**
	 * 产品上传 
	 * @param p
	 * @param AssociatedId 附件ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/AddProduct")
	@ResponseBody
	public AjaxRes AddProduct(Product p,String AssociatedId)throws Exception{
		AjaxRes res=new AjaxRes();
		try {
			ProductSer.insterFj(p,AssociatedId);
			res.setResMsg("产品上传成功");
		} catch (Exception e) {
			res.setResMsg("产品上传失败");
		}
		return res;
	}
	
	
	/**
	 * 查询产品信息
	 * @param p
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/FindProduct")
	@ResponseBody
	public AjaxRes FindProduct(Product p,Page<Product> page,String isyj)throws Exception{
		AjaxRes res=new AjaxRes();
		try {
			if(isyj!=null ){
				Page<Product> ProductList=ProductSer.RecommendProduct(p,new Page<Product>(),p.getUserIndustryId());
				System.out.println("ProductList-------"+ProductList);
				res.setSucceed(ProductList, "查询数据成功");
			}else{
				Page<Product> ProductList=ProductSer.findByPageIndustryId(p, page,"aaa", p.getUserIndustryId());
				System.out.println("ProductList-------"+ProductList);
				res.setSucceed(ProductList, "查询数据成功");
			}
		} catch (Exception e) {
			res.setResMsg("查询数据失败");
		}
		return res;
	}
	
	/**
	 * 产品详情
	 * @return
	 */
	@RequestMapping("/related")
	public ModelAndView related(Product p){
		ModelAndView mv=new ModelAndView();
		Product pro=ProductSer.findByProId(p);
		mv.addObject("pro", pro);
		mv.setViewName("FrontEnd/related");
		return mv;
	}
	

	/**
	 * 
	 * @param p
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/Docking")
	@ResponseBody
	public AjaxRes Docking(ProductDocking pd)throws Exception{
		AjaxRes res=new AjaxRes();
		//查询对接表判断当前用户是否已对接
		HttpSession session=getRequest().getSession();
		Users user=(Users)session.getAttribute("FrontUser");
		pd.setUserInformation(user.getUsersId());
		List<ProductDocking> pdList=DockingServ.find(pd);
		try {
			if(pdList.size()>0){
				//返回信息提示已对接
				res.setResMsg("您已和这个产品对接");
			}else{
				//对接，添加对接表 
				pd.setClickTime(DateUtils.getDate("yyyy/MM/dd HH:mm:ss"));
				pd.setProductDockingId(UuidUtil.get32UUID());
				System.out.println("查询之前的数据-----------"+pd);
				DockingServ.insert(pd);
				res.setResMsg("对接成功");
			}
		} catch (Exception e) {
			res.setResMsg("对接失败");
		}
		return res;
	}
	
	
	/**
	 * 查询审核状态产品信息
	 * @param p
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/AuditProduct")
	@ResponseBody
	public AjaxRes AuditProduct(Product p,Page<Product> page)throws Exception{
		AjaxRes res=new AjaxRes();
		Page<Product> ProductList=ProductSer.findByPageIndustryId(p,new Page<Product>(),null,p.getUserIndustryId());
		System.out.println("ProductList-------"+ProductList);
		res.setSucceed(ProductList, "查询数据成功");
		return res;
	}
	
}
